<!--
  移动签批->公文详情->收文->修改公文要素
-->
<template>
	<view>
		<van-popup v-model="showModel" closeable round position="bottom" :style="{ height: '100%' }"
			@click-close-icon="toClose" @click-overlay="toClose">
			<van-row type="flex" justify="center">
				<van-col offset="1" span="8">
					<van-cell value="修改公文要素" />
				</van-col>
			</van-row>
			<van-form>
				<van-collapse v-model="tableShow" accordion :border="border">
					<van-collapse-item icon="label-o" title="公文要素" title-class="text-bold" name="jbxx">
					
						<van-cell is-link required title="密级" :value="this.dict.getDictNameByDictCode('mj',formData.secrecyLevel)" @click="showMj = true" />
						<van-action-sheet v-model="showMj" :actions="mj" cancel-text="取消" @select="onSelectMj" />
						
						<van-cell is-link required title="紧急程度" :value="this.dict.getDictNameByDictCode('gljswjjcd',formData.emergencyLevel)" @click="showJjcd = true" />
						<van-action-sheet v-model="showJjcd" :actions="jjcd" cancel-text="取消" @select="onSelectJjcd" />
						
						<van-field v-model="formData.title" required label="标题" input-align="right" autosize rows="1" type="textarea" />
						
						<van-field label="办理时限" :value="formData.limitDate" @click="showBlsx = true" input-align="right" is-link/>
						<van-calendar v-model="showBlsx" :show-confirm="false" @confirm="onConfirmLimitDate" />
						
						<van-cell title="长期关注收文"> 
							<van-radio-group v-model="formData.isConcern" direction="horizontal" @change="onChangeConcern" class="radio_float">
								<van-radio name="1">是</van-radio>
								<van-radio name="0">否</van-radio>
							</van-radio-group>
						</van-cell>
						
						<van-field label="来文单位" v-model="formData.fwDeptName" required input-align="right" />
						
						<van-cell is-link title="来文编号" v-model="formData.fwcode" @click="showGenerate('lw')" />
						<generate @closeGenerate="closeGenerate" @saveGenerate="saveGenerate" :tagType="tagType" :showGenerate="showLw" :generateParamObject="generateParamObject"></generate>
						
						<van-cell is-link required title="收文编号" v-model="formData.swcode" @click="showGenerate('sw')" />
						<generate @closeGenerate="closeGenerate" @saveGenerate="saveGenerate" :tagType="tagType" :showGenerate="showSw" :generateParamObject="generateParamObject"></generate>
						
						<van-field label="收文时间" :value="formData.swTime" @click="showSwsj = true" input-align="right" is-link/>
						<van-calendar v-model="showSwsj" :show-confirm="false" @confirm="onConfirmSwsj" />
						
						<van-field v-model="formData.remark" label="备注" rows="2" autosize type="textarea" input-align="right" />
						
						<!-- 移动端不涉及文件上传和下载操作，因此修改公文要素隐藏这部分内容 -->
						<!-- <van-cell title="正文" />
						<van-field v-model="message" rows="1" autosize label="" type="textarea" readonly />
						<van-cell title="附件" value="民航旅客便利化实施指南.docx" value-class="text-sm text-blue" />
						<van-cell title="相关材料" value="相关材料.docx" value-class="text-sm text-blue" /> -->
					</van-collapse-item>
				</van-collapse>
			</van-form>

			<view class="padding-top-lg padding-bottom-lg">
				<van-row type="flex" justify="space-around">
					<van-col span="10">
						<van-button type="info" round plain hairline block @click="toClose">取消</van-button>
					</van-col>
					<van-col span="10">
						<van-button type="info" round block @click="toEdit">确定</van-button>
					</van-col>
				</van-row>
			</view>
		</van-popup>
	</view>
</template>

<script>
	import generate from 'pages/admin/components/receiveDoc/compo-generate';
	export default {
		
		components: {
			generate
		},
		props: {
			showEdit: {
				type: Boolean,
				default: false
			},
			detailObj:{ // 公文详情数据
				type:Object
			}
		},
		data() {
			return {
				showModel: false, // 显示组件
				formData: {},
				// 密级
				showMj: false,
				mj: [{
					name: '无'
					}, {
						name: '普通加密'
					}, {
						name: '特殊加密'
					}
				],
				// 紧急程度
				showJjcd: false,
				jjcd: [{
						name: '平件'
					}, {
						name: '急件'
					}, {
						name: '特急'
					}
				],
				// 办理时限
				showBlsx:false,
				// 来文/收文字号
				showLw: false,
				showSw: false,
				tagType: '',
				generateParamObject: {},
				// 收文时间
				showSwsj: false,
				
				// 当前展开面板的 name
				tableShow: 'jbxx',
				// 是否显示外边框,默认true
				border: false, 
			};
		},
		mounted() {
			// 获取字典项
			this.getDict()
		},
		watch: {
			showEdit(val) {
				this.showModel = val;
			},
			detailObj(val) {
				this.formData = val;
				this.generateParamObject.swWord = val.swWord;
				this.generateParamObject.swWordId = val.swWordId;
				this.generateParamObject.swYear = val.swYear;
				this.generateParamObject.swNum = val.swNum;
				this.generateParamObject.fwWord = val.fwWord;
				this.generateParamObject.fwYear = val.fwYear;
				this.generateParamObject.fwNum = val.fwNum;
			}
		},
		methods: {
			// 密级
			onSelectMj(item) {
				// 默认情况下点击选项时不会自动收起
				// 可以通过 close-on-click-action 属性开启自动收起
				this.formData.secrecyLevel=item.dictItemCode
				// this.formData.secrecyLevelName = item.name
				this.showMj = false;
			},
			// 紧急程度
			onSelectJjcd(item) {
				console.log(item)
				this.showJjcd = false;
				this.formData.emergencyLevel=item.dictItemCode
			},
			// 办理时限
			onConfirmLimitDate(date){
				const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
				this.showBlsx = false
				this.formData.limitDate = formatDate(date)
			},
		
			// 长期关注收文
			onChangeConcern(item) {
				console.log(item)
				this.formData.isConcern = item
			},
					
			// 显示字号组件
			showGenerate(type) {
				if(type == 'lw') {
					this.tagType = 'lw'
					this.showLw = true
				}
				if(type == 'sw'){
					this.tagType = 'sw'
					this.showSw = true
				}
			},
			// 关闭字号组件
			closeGenerate() {
				this.showLw = false
				this.showSw = false
			},
			// 保存字号
			saveGenerate(data) {
				if(data.type == 'lw') {
					this.formData.fwWord = data.lwWord
					this.formData.fwYear = data.lwYear
					this.formData.fwNum = data.lwNum
					this.formData.fwcode = this.generateCode(data.lwWord, data.lwYear, data.lwNum)
					this.showLw = false
				}
				if(data.type == 'sw') {
					this.formData.swWord = data.swWord
					this.formData.swYear = data.swYear
					this.formData.swNum = data.swNum
					this.formData.swWordId = data.swWordId
					this.formData.swcode = this.generateCode(data.swWord, data.swYear, data.swNum)
					this.showSw = false
				}
			},
			// 生成编号
			generateCode(word, year, num) {
				if(word != null && word != '' 
						&& year != null && year != ''
						&& num != null && num != ''){
					return word + '〔' + year + '〕' + num + '号'
				}
			},					
			
			// 收文时间
			onConfirmSwsj(date){
				const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
				this.showSwsj = false
				this.formData.swTime = formatDate(date)
			},
			
			// 取消按钮
			toClose(){
				this.$emit('closeEdit', this.showModel);
			},
			
			// 确定按钮
			toEdit(){
				this.$dialog.confirm({
				  confirmButtonColor: '#1989fa',
				  title: '系统提示',
				  message: '确认修改吗?',
				}).then(() => {
					if(this.validateForm()){
						this.saveElement()
					}
				}).catch(() => {
					// oncancel
				})
			},
			
			// 校验表单
			validateForm() {
				console.log("------校验表单==>: " + JSON.stringify(this.formData));
        // 密级
        if(this.util.isEmpty(this.formData.secrecyLevel)) {
          this.$toast("请选择密级")
          return false
        }
        // 紧急程度
        if(this.util.isEmpty(this.formData.emergencyLevel)) {
          this.$toast("请选择紧急程度")
          return false
        }
        // 标题
        if(this.util.isEmpty(this.formData.title)) {
          this.$toast("请填写标题")
          return false
        }
        // 来文单位
        if(this.util.isEmpty(this.formData.fwDeptName)) {
          this.$toast("请填写来文单位")
          return false
        }
        // 收文编号
        if(this.util.isEmpty(this.formData.swcode)) {
          this.$toast("请填写收文编号")
          return false
        }
				return true
			},
			
			// 修改公文要素
			saveElement(){
				var _this = this
				this.$httploading({
				  url: this.$http.adornUrl(`/caac/oa/authority/sw?systemKey=`+this.cache.get('userInfo').systemKey+`&systemFlag=`+this.cache.get('userInfo').systemFlag),
				  method: 'post',
				  data:JSON.stringify(this.formData)				  
				}).then(({data}) => {
					console.log("修改公文要素data: " + JSON.stringify(data));
					if(data.code==200){
						_this.$toast('修改公文要素成功')
						_this.$emit('closeEdit', this.showModel);
						_this.$emit('refreshBtnGroup',true) //修改成功后通知父组件重新加载数据
					}
				})
			},
			
			//父组件点击修改公文要素时调用获取详情回显到表单
			editGw(proInstId){				
				console.log("获取公文要素详情"+proInstId);
				var _this = this
				this.$httploading({
				  url: this.$http.adornUrl(`/caac/oa/authority/gljfw/proInstId/`+proInstId),
				  method: 'get',
				  params: this.$http.adornParams({
					  'systemKey': this.cache.get('userInfo').systemKey,//固定值
					  'systemFlag': this.cache.get('userInfo').systemFlag//固定值
					  // 'systemKey': 'ls_liu',//固定值
					  // 'systemFlag': 'yidong'//固定值
				  })
				}).then(({data}) => {
					console.log("获取公文要素详情data: " + JSON.stringify(data));
					if(data.code==200){
						_this.formData = data.data
						_this.getRelation(data.data.extendIdForRelate)
					}
				})
			},
			
			
			//获取需要的字典项
			getDict(){
				this.dictSecrecyLevel = this.cache.get('mj')//密级
				//给van-action-sheet 赋值
				this.mj=[]
				this.dictSecrecyLevel.forEach((item)=>{
					this.mj.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})
				
				this.dictEmergencyLevel = this.cache.get('gljswjjcd')//紧急程度
				this.jjcd=[]
				this.dictEmergencyLevel.forEach((item)=>{
					this.jjcd.push({name:item.dictItemName,dictItemCode:item.dictItemCode})
				})
				
			},
			
			
		},
		
		
	};
</script>

<style scoped>
	
	.van-cell__title {
		min-width: 55%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-collapse-item__content {
		padding: 0px;
		color: #969799;
		font-size: 14px;
		line-height: 1.5;
		background-color: #fff;
	}

	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;
		
		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}
	
	.radio_float {
		float: right;
	}
</style>
